package com.carrot.api.controller.system;

import com.carrot.common.core.controller.BaseController;
import com.carrot.common.core.domain.AjaxResult;
import com.carrot.common.core.domain.dto.PointParserDto;
import com.carrot.common.core.domain.vo.IotKvPairVo;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.utils.StringUtils;
import com.carrot.system.service.IPointCheckService;
import jakarta.annotation.security.PermitAll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 点表验证
 *
 * @author 刘红真
 */
@RestController
@RequestMapping("/system/point-check")
public class SysPointCheckController extends BaseController {

    @Autowired
    private IPointCheckService pointCheckService;

    /**
     * IEC104
     *
     * @param dto
     * @return
     */
    @PermitAll
    @PostMapping("iec104")
    public AjaxResult iec(@RequestBody PointParserDto dto) {

        if (StringUtils.isAnyEmpty(dto.getServerIp(), dto.getMetadata(), dto.getMessage())) {
            throw new ServiceException("参数校验失败");
        }
        if (null == dto.getServerPort()) {
            throw new ServiceException("参数校验失败");
        }

        List<IotKvPairVo> kvPairList = pointCheckService.iec104(dto);

        return AjaxResult.success(kvPairList);
    }

    /**
     * MQTT
     *
     * @param dto
     * @return
     */
    @PermitAll
    @PostMapping("mqtt")
    public AjaxResult mqtt(@RequestBody PointParserDto dto) {

        if (StringUtils.isAnyEmpty(dto.getClientIp(), dto.getServerIp(), dto.getMetadata(), dto.getMessage())) {
            throw new ServiceException("参数校验失败");
        }
        if (null == dto.getServerPort()) {
            throw new ServiceException("参数校验失败");
        }

        List<IotKvPairVo> kvPairList = pointCheckService.mqtt(dto);

        return AjaxResult.success(kvPairList);
    }
}
